const CONFIG = require('../../config.js');
var startPoint;
//获取应用实例
const app = getApp()

Page({
  
  onShareAppMessage() {
    return {
      title: 'swiper',
      path: 'page/component/pages/swiper/swiper'
    }
  },

  data: {
		hostUrl: CONFIG.url,
		userInfo: {},
		hasUserInfo: false,
		background: ['demo-text-1'],
		indicatorDots: true,
		vertical: false,
		autoplay: true,
		interval: 4000,
		duration: 500,
		noteList: [],
		loginInfo: null,
		typeLevel1List: [
			{typeLevel1Name: '房产信息', typeLevel1Code: '100'},
			{typeLevel1Name: '企业信息', typeLevel1Code: '200'},
			{typeLevel1Name: '信用信息', typeLevel1Code: '300'},
			{typeLevel1Name: '车辆信息', typeLevel1Code: '400'}
		],
		typeLevelList: [
			[
				{ name: '房产一信息', value: '10001' },
				{ name: '房产二信息', value: '10002' },
			],
			[
				{ name: '企业税信息', value: '20001' },
				{ name: '企业发票信息', value: '20002' },
			],
			[
				{ name: '房供贷', value: '30001' },
				{ name: '公鸡贷', value: '30002' },
				{ name: '车信贷', value: '30004' },
				{ name: '保单贷', value: '30005' },
				{ name: '薪水贷', value: '30006' },
			],
			[{ name: '车辆抵押', value: '40001' }]
		],
		userScore: [],
		isShow: true,
		isTrue: '1',	// 信息
		cardTextList: {
			name0: '房产信息',
			name1: '企业信息',
			name2: '信用信息',
			name3: '车辆信息',
		},
		buttonTop: 500,
		buttonLeft: 300,
		gzhUrl: "https://wx.huidailang.cn//uploadPath/wxImg/hdlgzh.jpg",
		isGzhShow: true,

		textList: {
			yiya: '\u4e00\u62bc',
			erya: '\u4e8c\u62bc',
			diya: '\u62b5\u62bc',
			zhudai: '\u52a9\u8d37',
			dai: '\u8d37',
			daikuan: '\u8d37\u6b3e',
		}
  },

	//   关闭评分动画
	closeFn:function (){
		this.setData({
			isShow: true
		});
	},

	// 执行动画，跳转到评分页面
	toScoreFn(){
		if(this.data.isTrue === '0'){
			this.setData({
				isShow: false
			});
			setTimeout(()=>{
				this.closeFn();
				wx.navigateTo({
				  url: '/pages/score/index',
				})
			}, 3000)
		}else{
			wx.navigateTo({
			  url: '/pages/score/index',
			})
		}
	},

	// 获取资讯数据
	getTipsData(){
		wx.request({
			url: CONFIG.host + "/system/notice/getNoteList",
			data: {},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					this.setData({
						noteList: res.data.data,
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},
	
	// 获取用户评分
	getUserScoreData(){
		wx.request({
			url: CONFIG.host + "system/appRiskInfoController/getRiskUserInfo",
			data: {},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					this.setData({
						userScore: res.data.data,
					})
					console.log(this.data.userScore,1122);
				}else {
					this.setData({
						userScore: false,
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 产品选择
	productSelectFn(e){
		let idx = e.currentTarget.dataset.value - 1;
		let productSelected = this.data.typeLevelList[idx];
		let that = this;
		if(this.data.isTrue === '1'){
			wx.navigateTo({
				url: '/pages/indexEdit/index?proId=' + e.currentTarget.dataset.value,
			})
			return;
		}
		wx.showActionSheet({
			itemList: productSelected.map((item) => item.name),
			success (res) {
				let productTypePid = that.data.typeLevel1List[idx].typeLevel1Code;
				let productTypeid = productSelected[res.tapIndex].value;
				let params = {
					productTypeid: productTypeid,
					productTypePid: productTypePid,
				}
				wx.setStorage({
					key:"productInfo",
  					data: JSON.stringify(params)
				})
				wx.navigateTo({
					url: '/pages/productList/productList?productTypePid=' + productTypePid + '&productTypeid=' + productTypeid,
				})
			},
			fail (res) {
				console.log(res.errMsg)
			}
		})
	},

	// 获取页面显示数据
	getShowData(){
		wx.request({
			url: CONFIG.host + "system/appTestController/getTest",
			data: {},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success'){
					// res.data.data = '0';
					if(res.data.data === '0'){
						this.setData({
							isTrue: res.data.data,
							[`cardTextList.name0`] : '房产' + this.data.textList.diya,
							[`cardTextList.name1`] : '企业' + this.data.textList.zhudai,
							[`cardTextList.name2`] : '信用' + this.data.textList.zhudai,
							[`cardTextList.name3`] : '车辆' + this.data.textList.diya,
							[`typeLevel1List[0][0].name`] : '房产' + this.data.textList.yiya + this.data.textList.dai,
							[`typeLevel1List[0][1].name`] : '企业' + this.data.textList.zhudai,
							[`typeLevel1List[0][2].name`] : '信用' + this.data.textList.zhudai,
							[`typeLevel1List[0][3].name`] : '房产' + this.data.textList.yiya + this.data.textList.dai,
							[`typeLevelList[0][0].name`] : '房产' + this.data.textList.yiya,
							[`typeLevelList[0][1].name`] : '房产' + this.data.textList.erya,
							[`typeLevelList[1][0].name`] : '企业税' + this.data.textList.dai,
							[`typeLevelList[1][1].name`] : '企业发票' + this.data.textList.dai,
							[`typeLevelList[2][0].name`] : '房供' + this.data.textList.dai,
							[`typeLevelList[2][1].name`] : '公鸡' + this.data.textList.dai,
							[`typeLevelList[2][2].name`] : '车信' + this.data.textList.dai,
							[`typeLevelList[2][3].name`] : '保单' + this.data.textList.dai,
							[`typeLevelList[2][4].name`] : '薪水' + this.data.textList.dai,
							[`typeLevelList[3][0].name`] : '车辆' + this.data.textList.diya,
						})
					}else{
						this.setData({
							isTrue: res.data.data,
						})
					}
					wx.setStorageSync('showUser', res.data.data);
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	buttonStart: function (e) {
		startPoint = e.touches[0]
	},
	buttonMove: function (e) {
		if(!startPoint) return;
		var endPoint = e.touches[e.touches.length - 1];
		var translateX = endPoint.clientX - startPoint.clientX;
		var translateY = endPoint.clientY - startPoint.clientY;
		startPoint = endPoint
		var buttonTop = this.data.buttonTop + translateY
		var buttonLeft = this.data.buttonLeft + translateX
		//判断是移动否超出屏幕
		if (buttonLeft+50 >= this.data.windowWidth){
			buttonLeft = this.data.windowWidth-50;
		}
		if (buttonLeft<=0){
			buttonLeft=0;
		}
		if (buttonTop<=0){
			buttonTop=0
		}
		if (buttonTop + 50 >= this.data.windowHeight){
			buttonTop = this.data.windowHeight-50;
		}
		this.setData({
			buttonTop: buttonTop,
			buttonLeft: buttonLeft
		})
	},

	// 显示大图
	shoeBigImg(e){
		startPoint = e.touches[0];
		this.setData({
			isGzhShow : false
		})
	},

	hideGzh(){
		this.setData({
			isGzhShow : true
		})
	},

	onLoad: function () {
		let loginInfo = wx.getStorageSync('loginInfo') || '';
		if(loginInfo){
			this.setData({
				loginInfo : JSON.parse(loginInfo)
			})
			this.getShowData();
		}else {
			wx.redirectTo ({
				url: '/pages/login/login'
			})
		}

		var that =this;
		wx.getSystemInfo({
		success: function (res) {
			console.log(res);
			// 屏幕宽度、高度
			console.log('height=' + res.windowHeight);
			console.log('width=' + res.windowWidth);
			// 高度,宽度 单位为px
			that.setData({
				windowHeight:  res.windowHeight,
				windowWidth:  res.windowWidth
			})
		}
		})
  	},

	onShow:function() {
		let loginInfo = wx.getStorageSync('loginInfo') || '';
		if(loginInfo){
			this.getUserScoreData();
			this.getTipsData();
		}
	},
})
